﻿using System;
using System.Collections.Specialized;
using System.Web.UI;

namespace AspDotnetPractice.CompositeControl
{
    public class Rendered : Control, IPostBackDataHandler, IPostBackEventHandler
    {
        private String text1;
        private String text2;
        private String text = "Press the button to see if you won.";
        private int number = 100;


        private int Sum
        {
            get
            {
                return Int32.Parse(text1) +
                   Int32.Parse(text2);
            }
        }

        public int Number
        {
            get
            {
                return number;
            }
            set
            {
                number = value;
            }
        }


        public String Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public event CheckEventHandler Check;

        protected virtual void OnCheck(CheckEventArgs ce)
        {
            if (Check != null)
            {
                Check(this, ce);
            }
        }

        public virtual bool LoadPostData(string postDataKey,NameValueCollection values)
        {
            text1 = values[UniqueID + "t1"];
            text2 = values[UniqueID + "t2"];
            Page.RegisterRequiresRaiseEvent(this);
            return false;
        }

        public virtual void RaisePostDataChangedEvent()
        {

        }

        public void RaisePostBackEvent(string eventArgument)
        {
            OnCheck(new CheckEventArgs(Sum - Number));
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.Write("<h3>Enter a number : ");
            output.Write("<INPUT type= text name = " + this.UniqueID + "t1" +
            " value = '0' >");
            output.Write("</h3><br>");
            output.Write("<br><h3>Enter another number : ");
            output.Write("<INPUT type= text name = " + this.UniqueID + "t2" +
               " value =  '0' >");
            output.Write("</h3><br>");
            output.Write("<br><br><input type= submit name = " +
               this.UniqueID + " value= 'Submit'>");
            output.Write("<br><br><span style='height:50px;width:500px;'>"
               + Text + "</span>");
        }
    }     


}